home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Textfiles / Programmer's Digest 1.0.sit / The Programmers Digest™ 1.0.rsrc / TEXT_131.txt < prev    next >
Text File  |  1998-11-11  |  8KB  |  163 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.                                                       ¬†
  16.  
  17.  
  18. Edited by: ZiffDuck
  19.  
  20.                 For any of you who have read "Mac Programming for Dummies" by Dan Parks Sydow. You will recognize a lot of this text. Why did I compile information from this book? Well, it is one of the best books for learning how to program on the Mac, and when I was learning (and I still am =) I followed every step, reread things and well pretty much read all the fine print.
  21.  
  22.             In case you are wondering why this article is first and not last or the middle is a good question to ask. Let me explain that topic to you. You see the toolbox is not an application, or lines of code. The toolbox is one of the very special things about the Macintosh computer; Apple has built in thousands and thousands of Mini programs. You can access these mini programs by just typing one line of code. Now these mini programs make your life easier thanks to those brilliant people at Apple. They make mini tasks easy because there already made for you. Now imagine your Mac is the toolbox, all the mini programs are the tools, and together make your Mac toolbox.
  23.  
  24.             Calls to the toolbox make it easier to write an application when you are working with windows, fonts, and menus you name it! Apple has made these all for you so you don‚Äôt have to write an extra 50 lines of code. Were you aware that there are more then five thousand Toolbox calls? Were you also aware of the Toolbox when you bought your Macintosh? Probably not, but some people are aware, that is why a lot of people like to program on the Macintosh. By now you may or may not have figured out that there are no such things called "Mini programs" they are in fact called "Functions" 
  25.  
  26. GetNewWindow, MoveTo, DrawString. And any other toolbox call is a function.
  27.  
  28. Function Parameters 
  29. Parameters (pronounced Pah-Ram-ah-ter) or Arguments (take your pick) provide the toolbox with information that it needs to supply to the toolbox so it can run properly. Some toolbox functions need more than one parameter, and some functions don't need any. All parameters are contained in parenthesis
  30.  
  31. Initialization Functions
  32. There are eight Initialization functions you must include when writing a Macintosh program. They go after you declare you variables in your source code. Here I will go over these eight topics.
  33.  
  34. The Functions
  35. InitGraf( &qd.thePort );
  36. InitFonts();
  37. InitWindows();
  38. InitMenus();
  39. TEInit();
  40. InitDialogs( OL );
  41. FlushEvents( EveryEvent, 0 );
  42. InitCursor();
  43.  
  44.  
  45. Displaying a Window
  46. Just about everything in a Mac program is contained in a window, or a menu. You can guess why those calls are one of the most important Toolbox calls in Macintosh programming. The code below is what it will usually look like when you make a call to this function
  47.  
  48. WindowPtr        theWindow;
  49. theWindow = GetNewWindow( 128, OL, (WindowPtr)-1L );
  50.  
  51.  
  52. Activating a Window
  53. Once you have made this beautiful, but plain window you want to do something with it. You don't want a plain window just sitting in your program, right? Well with this next section it will explain how to put text and pictures into a window.
  54.  
  55. SetPort( theWindow );
  56.  
  57. SetPort helps the Mac realize what window you are going to make it draw to. To help SetPort enable its function you pass it along to WindowPtr of the window to make the window current. You can use the WindowPtr variable that was returned by the call to GetNewWindow.
  58.  
  59.  
  60. Displaying a MenuBar 
  61. In this call there are going to be one variable and three toolbox calls for you to display a menubar
  62.  
  63. Handle menuBarHandle;
  64.  
  65. menuBarHandle = GetNewMBar( 128 );
  66. SetMenuBar( menuBarHandle );
  67. DrawMenuBar();
  68.  
  69. This part of the code needs a little explaining‚Ķ GetNewMBar is the call that handles the MBAR resources ID. Also GetNewMBar gives the program a handle. Now, SetMenuBar needs this handle in order to group all the information about the menubar and all the menus contained in it. DrawMenuBar is what makes the menu bar visible on your screen. 
  70.  
  71.  
  72. Capturing Events
  73. You know how old men just kind of sit on their front porches widdlin' their sticks? Well WaitNextEvent is just like those old men. It just kind of sits there. It looks for an event to happen. When it notices this event is stores the information about the variable in EventRecord 
  74.  
  75. EventRecord        theEvent;
  76. WaitNextEvent( everyEvent, &theEvent, OL, OL );
  77.  
  78. an event is a variety of things that happens when the user does something, clicks the menu, clicks a window or even when the user clicks the mouse button.
  79.  
  80.  
  81. Locating a Mouse Click
  82. Usually, a user can click anywhere on the screen. But, it's up to your program that you code to figure out where the cursor is when the user makes a click. For your program to do this you must use FindWindow
  83.  
  84. EventRecord        TheEvent;
  85. WindowPtr             whichWindow;
  86. short                                thePart
  87.  
  88. thePart  =  FindWindow( theEvent.where, &whichWindow );
  89.  
  90. The variable of EventRecord is how FindWindow operates, so you must call what happens after WaitNextEvent. In overall, FindWindow gives your program the location of the screen, window or where ever the mouse was clicked. If the mouse happens to be clicked in a window, FindWindow uses the variable whichWindow to provide your program with a WindowPtr to the window.
  91.  
  92.  
  93. Working With Windows
  94. When/if FindWindow tells your homemade computer application that a user clicked somewhere in the drag bar of a window, you should respond by using the DragWindow. This function watches the mouse movements as the user moves the little mousie.
  95.  
  96. WindowPtr                    whichWindow;
  97. EventRecord      theEvent;
  98.  
  99. DragWindow( whichWindow, theEvent.where,
  100.                                                 &qd.screenBits.bounds );
  101.  
  102. The first function of DragWindow tells the toolbox what window to drag. The second parameter is where the mouse was first clicked. The last parameter lets the toolbox know that the window can be dragged anywhere on the screen.
  103.  
  104. If FindWindow tells your application that the user clicked in the close box, you should call DisposeWindow to well, dispose the window.
  105.  
  106. WindowPtr            whichWindow;
  107.  
  108. DisposeWindow( whichWindow );
  109.  
  110.  
  111. Managing Menus
  112. Whenever a user clicks in the menu bar, a call to MenuSelect will take care of making menus drop when the mouse is dragged over them. When the mouse is clicked, a call to MenuSelect just happens to know what item and menu is selected. It stores all its valuable information in menuAndItem.
  113.  
  114. long        menuAndItem;
  115. MenuAndItem = MenuSelect( theEvent.where );
  116.  
  117. To extract the number of the menu and menu item call HiWord and  LoWord from menuAndItem 
  118.  
  119. short        theMenu;
  120. short        theMenuItem;
  121. theMenu  = HiWord( menuAndItem );
  122. theMenuItem = LoWord( menuAndItem );
  123.  
  124. When you call to MenuSelect it selects a menu in the menubar. After all this, call to HiliteMenu to return the menu name to its original place.
  125.  
  126. HiLiteMenu (0);
  127.  
  128.  
  129. Drawing Text 
  130. On the Macintosh text is drawn not written. The toolbox function DrawString draws a word or line of text to a window. Enclose the written text in double quotes and precede the first word with the backslash key "\" and the letter p.
  131.  
  132. DrawString( "\pThis is where you put your text :-)" );
  133.  
  134. to select where in the window the text is drawn call the function MoveTo, then draw string
  135.  
  136. MoveTo( 20, 50 );
  137. DrawString( "\pThis is where you put your text :-)" );
  138.  
  139.  
  140. Drawing Shapes
  141. Did you know you can also use the toolbox to make a rectangle, or fill it with a color or pattern? Well now you do! To do this you must call FrameRect to obviously draw a rectangle. Or you can call FillRect to fill the rectangle with a pattern. But you must tell the toolbox where the rectangle will go and how big or small it will be.
  142.  
  143. Rect        theRect;
  144.  
  145. Set( &theRect, 0, 0, 400, 280 );
  146.  
  147. You must be sure not mix the parameters of SetRect. You can follow them in this order
  148.  
  149. SetRect( &theRect, left, top, right, bottom );
  150.  
  151. after the size and location have been set, call FrameRect to draw a black line around it.
  152.  
  153. Framerect( &theRect );
  154.  
  155. If you want to fill the rectangle. Call FillRect. The second parameter tells the Toolbox what pattern to be filled with. here is how to fill it with a gray pattern
  156.  
  157. FillRect( &theRect, &qd.Gray );
  158.  
  159. other colors included are black, white, gray, ltgray, dkgray (always put &qd before the colors!)
  160.  
  161. ____________________________________________________________
  162. Well people, that concludes my article in the Macintosh toolbox, if you have any comments, questions or ideas please email me or any one else on the team. Once again thanks! -Logik
  163.